﻿using GalaSoft.MvvmLight.Command;
using Glorysoft.Auto.Xfw;
using Glorysoft.EAP.EAPClient.Common;
using Serilog.Core;
using System;
using System.Windows;

namespace Glorysoft.EAP.EAPClient.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class TrackOutLoginViewModel : PopupWindowVM
    {
        public static string trackoutpasswork;
        private readonly Logger ServiceLogger = XfwContext.EapLogger;
        public delegate void CloseDelegate();
        public event CloseDelegate CloseEvent;
        /// <summary>
        /// Initializes a new instance of the LoginViewModel class.
        /// </summary>
        string EQPPORT = string.Empty;
        public TrackOutLoginViewModel(string EQPandPort)                                                                                                                            
        {
            EQPPORT = EQPandPort;
        }
        private string name;
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                RaisePropertyChanged("Name");
            }
        }
        private string password;
        public string Password
        {
            get { return password; } 
            set
            {
                password = value;
                trackoutpasswork = value;
                RaisePropertyChanged("Password");
            }
        }

        #region command
        private RelayCommand<string> trackLoginCmd;
        public RelayCommand<string> TrackLoginCmd
        {
            get
            {
                return trackLoginCmd ?? (trackLoginCmd = new RelayCommand<string>(TrackOutLogin));
            }
        }

        private RelayCommand logOutCmd;
        public RelayCommand LogOutCmd
        {
            get
            {
                return logOutCmd ?? (logOutCmd = new RelayCommand(LogOut));
            }
        }
        public void LogOut()
        {
            try
            {
                if (CloseEvent != null)
                    CloseEvent();
            }
            catch (Exception ex)
            {
                ServiceLogger.Error(string.Format("{0}: {1}", GetType().Name, ex));
            }
        }
        public void TrackOutLogin(string obj)
        {
            try
            {
                if (string.IsNullOrEmpty(Name.Trim()))
                {
                    MessageBox.Show(Consts.GetTranslation("Msg_UserIdInputInfo"), Consts.GetTranslation("Msg_ConfirmInfo"), MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                if (string.IsNullOrEmpty(Password))
                {
                    MessageBox.Show(Consts.GetTranslation("Msg_UserPwdInputInfo"), Consts.GetTranslation("Msg_ConfirmInfo"), MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                //Client2Login(Name.Trim().ToUpper(), Password);
                Controller.ShowSubWindow(new TrackOutLossViewModel());
                this.Close();
            }
            catch (Exception ex)
            {
                ServiceLogger.Error(string.Format("{0}: {1}", GetType().Name, ex));
            }
        }
        public void Client2Login(string Name, string password, string facilityId)
        {
            try
            {
                string eqpid = EQPPORT.Split('/')[0];
                string portid = EQPPORT.Split('/')[1];
                EAPClient client = new EAPClient();
                var _return = client.Login(Name, password,facilityId);
                if (_return != null && _return.ReturnCode == "OK")
                {
                    if (CMessageBox.Show(Consts.GetTranslation("Msg_TrackOutLot"), Consts.GetTranslation("Msg_ConfirmInfo"), CMessageBoxButton.OKCancel) == CMessageBoxResult.OK)
                    {
                        EAPClient eap = new EAPClient();
                        //eap.TrackOutByUser(eqpid);
                    }
                    this.Close();
                }
                else
                {
                    CMessageBox.Show(Consts.GetTranslation("Msg_Loginfail"));
                }
            }
            catch (Exception ex)
            {
                ServiceLogger.Error(string.Format("{0}: {1}", GetType().Name, ex));
            }
        }
        private RelayCommand cancelCmd;
        public RelayCommand CancelCmd
        {
            get { return cancelCmd ?? (cancelCmd = new RelayCommand(Cancel)); }
        }
        public void Cancel()
        {
            try
            {
                if (CloseEvent != null)
                    CloseEvent();
            }
            catch (Exception ex)
            {
                ServiceLogger.Error(string.Format("{0}: {1}", GetType().Name, ex));
            }
            this.Close();
        }
        #endregion
    }
}